#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QHBoxLayout>
#include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    _options=0;
    ui->setupUi(this);
    initUI();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onOpen()
{
    QString name=QFileDialog::getOpenFileName();
    if(name.isEmpty())return;
    _editor->openFile(name);
}


void MainWindow::onSave(){}

void MainWindow::onSaveAs(){}

void MainWindow::onExie(){}

void MainWindow::onClose(){}

void MainWindow::onOptions()
{
    if(!_options)
    {
        _options=new Options(this);
        _options->setWindowFlag(Qt::Dialog);
        connect(_options,SIGNAL(sFontChange()),this,SLOT(onFontChange()));
    }
    _options->show();
}

void MainWindow::onFontChange()
{
    _editor->setFont(_options->_currentFont);
}

void MainWindow::initUI()
{
    _editor=new Editor(ui->centralWidget);
    QLayout*lay=new QHBoxLayout(ui->centralWidget);
    lay->addWidget(_editor);
    ui->centralWidget->setLayout(lay);

    connect(ui->actionOpen,SIGNAL(triggered(bool)),this,SLOT(onOpen()));
    connect(ui->actionSave,SIGNAL(triggered(bool)),this,SLOT(onSave()));
    connect(ui->actionSave_As,SIGNAL(triggered(bool)),this,SLOT(onSaveAs()));
    connect(ui->actionClose,SIGNAL(triggered(bool)),this,SLOT(onClose()));
    connect(ui->actionExit,SIGNAL(triggered(bool)),this,SLOT(onExie()));
    connect(ui->actionOptions,SIGNAL(triggered(bool)),this,SLOT(onOptions()));
}
